//
//  EMMineCardView.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/11.
//  Copyright © 2020 zh. All rights reserved.
//

#import "EMMineCardView.h"
#import "EMMineResponseModel.h"
#import <UIImage+YYWebImage.h>

@interface EMMineCardItem : UIView

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *textLabel;

@end

@implementation EMMineCardItem

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc] init];
        [self addSubview:self.imageView];
        self.textLabel = [[UILabel alloc] init];
        self.textLabel.font = FONT_PINGFANG_REGULAR_(12);
        self.textLabel.textColor = ColorFromRGB(0x999999);
        [self addSubview:self.textLabel];
        
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(24, 24));
        }];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageView.mas_bottom).offset(3);
            make.left.right.equalTo(self);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

@end

@interface EMMineCardView ()

@property (nonatomic, strong) EMMineCardItem *card1;
@property (nonatomic, strong) EMMineCardItem *card2;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;

@property (nonatomic, strong) EMMineResponseModel *model;

@end

@implementation EMMineCardView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.card1 = [[EMMineCardItem alloc] init];
        [self addSubview:self.card1];
        
        self.card2 = [[EMMineCardItem alloc] init];
        [self addSubview:self.card2];
        
        _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_btn1];
        [_btn1 addTarget:self action:@selector(clickBtn1:) forControlEvents:UIControlEventTouchUpInside];
        
        _btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_btn2];
        [_btn2 addTarget:self action:@selector(clickBtn2:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *breakView = [[UIView alloc] init];
        breakView.backgroundColor = ColorFromRGB(0xeeeeee);
        [self addSubview:breakView];
        [breakView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.height.mas_equalTo(36);
            make.width.mas_equalTo(1);
        }];
        
        [self.card1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.centerX.equalTo(self).multipliedBy(0.5);
        }];
        
        [self.card2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.centerX.equalTo(self).multipliedBy(1.5);
        }];
        
        [_btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self);
            make.right.equalTo(breakView.mas_left);
        }];
        
        [_btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(self);
            make.left.equalTo(breakView.mas_right);
        }];
    }
    return self;
}

- (void)clickBtn1:(UIButton *)sender
{
    if (self.callback) {
        self.callback(kMineCard1, self, self.model.leftAction);
    }
}

- (void)clickBtn2:(UIButton *)sender
{
    if (self.callback) {
        self.callback(kMineCard1, self, self.model.rightAction);
    }
}

- (void)updateWithDataModel:(id)dataModel callBack:(wdpCallBackBlock)callBack
{
    [super updateWithDataModel:dataModel callBack:callBack];
    self.model = (EMMineResponseModel *)dataModel;
    self.card1.textLabel.text = self.model.leftText;
    [self.card1.imageView yy_setImageWithURL:[NSURL URLWithString:SAFE_STRING(self.model.leftIcon)] placeholder:nil];
    self.card2.textLabel.text = self.model.rightText;
    [self.card2.imageView yy_setImageWithURL:[NSURL URLWithString:SAFE_STRING(self.model.rightIcon)] placeholder:nil];
}

@end
